package com.reptile.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.reptile.entity.YsNews;
import com.reptile.mapper.YsNewsMapper;
import com.reptile.service.YsNewsService;
import com.reptile.dto.NewsDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 新闻接口实现类
 *
 * @author songyafei
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class YsNewsServiceImpl extends ServiceImpl<YsNewsMapper, YsNews> implements YsNewsService {


    private final YsNewsMapper ysNewsMapper;

    /**
     * 保存新闻接口实现
     *
     * @param newsDTO
     * @return Integer
     */
    @Override
    public Integer save(NewsDTO newsDTO) {
        if (Objects.isNull(newsDTO)) {
            throw new BaseException(BaseEnum.DATA_IS_NULL);
        }
        YsNews ysNews = new YsNews();
        BeanUtils.copyProperties(newsDTO, ysNews);
        return ysNewsMapper.insert(ysNews);
    }

    /**
     * 查询新闻接口实现
     *
     * @param newsDTO 新闻对象
     * @return
     */
    @Override
    public List<YsNews> queryNews(NewsDTO newsDTO) {
        if (Objects.isNull(newsDTO)) {
            throw new BaseException(BaseEnum.FIND_PARAMETER_IS_NULL);
        }
        QueryWrapper<YsNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("new_type", newsDTO.getNewType());
        return ysNewsMapper.selectList(queryWrapper);
    }

    /**
     * 批量导入新闻
     *
     * @param data
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT)
    public Boolean saveNewsToDb(List<NewsDTO> data) {
        if (CollectionUtils.isEmpty(data)) {
            return false;
        }
        log.info("开始批量导入新闻：", JSON.toJSONString(data));
        try {
            // 进行拆解封装
            List<YsNews> newsData = data.stream().map(item -> {
                YsNews ysNews = new YsNews();
                BeanUtils.copyProperties(item, ysNews);
                return ysNews;
            }).collect(Collectors.toList());
            this.saveBatch(newsData);
        }catch (Exception e){
            log.error("批量导入新闻数据失败！{}",e);
            throw new BaseException(e);
        }
        log.info("批量导入完成！");
        return true;
    }

    /**
     * 批量删除重复新闻数据
     *
     * @return Boolean
     */
    @Transactional(isolation = Isolation.DEFAULT)
    @Override
    public Boolean deleteRepeatData() {
        List<Long> longs = ysNewsMapper.queryRepeatData();
        if (CollectionUtils.isEmpty(longs)) {
            log.info("本次未查到需要删除的新闻数据！程序结束");
            return true;
        }
        try {
            log.info("本次需要删除的新闻数据:{}", JSON.toJSONString(longs));
            return this.removeByIds(longs);
        } catch (Exception e) {
            log.error("删除新闻重复数据失败！{}",e);
            throw new BaseException(e);
        }
    }
}
